package com.q.tqb.activity.mine;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.q.tqb.R;
import com.q.tqb.activity.send.SendSpeakingActivity;
import com.q.tqb.base.AppConst;
import com.q.tqb.base.BaseActivity;
import com.q.tqb.model.WxPayData;
import com.q.tqb.util.LogUtil;
import com.q.tqb.util.SPUtil;
import com.q.tqb.util.StringUtil;
import com.q.tqb.util.http.IResponse;
import com.q.tqb.util.http.OkHttpUtil;
import com.q.tqb.view.dialog.PayDialog;

import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class BuyCoinsActivity extends BaseActivity {
    @BindView(R.id.et_num)
    EditText et_num;
    PayDialog payDialog;
    int payType;
    String orderId;

    @Override
    public int getLayoutId() {
        return R.layout.activity_buy_conins;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        setTitle("购买现金币");
    }

    @OnClick({R.id.iv_back, R.id.regist_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                this.finish();
                break;
            case R.id.regist_btn:
                doCharge();
                break;
        }
    }

    private void doCharge() {
        String rewardMoney = et_num.getText().toString();
        final double allMoney;
        allMoney = Double.valueOf(rewardMoney);
        if (allMoney <= 0) {
            showToast("请正确输入购买金币数量");
            return;
        }
        payDialog = new PayDialog(BuyCoinsActivity.this, allMoney, "");
        payDialog.setOnSettingListener(new PayDialog.CommonListener() {
            @Override
            public void onClick(int type) {
                if (type == 1) {
                    //不支持账户余额支付
                } else {
                    payType = 2;
                    payOrder();
                }
            }
        });
        payDialog.dimssCount();
        payDialog.show();
    }

    private void payOrder() {
        int uid = (int) SPUtil.get(this, "uid", 0);
        Map<String, String> map = new HashMap<>();
        map.put("uid", uid + "");
        map.put("token", StringUtil.getToken(uid));
        map.put("money", et_num.getText().toString());
        map.put("pay_type", payType + "");
        LogUtil.i(map.toString());
        mProgressDialog.show();
        OkHttpUtil.getInstance().post(this, AppConst.URL_BUY_MONEY, map, new IResponse() {
            @Override
            public void onSuccess(JsonElement element) {
                mProgressDialog.dismiss();
                try {
                    JSONObject jsonObject = new JSONObject(element.toString());
                    orderId = jsonObject.getString("order_id");
                    if (payType == 2) {
                        wxPay();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                LogUtil.i(errorMsg + "statusCode" + statusCode);
                showToast(errorMsg);
                mProgressDialog.dismiss();
            }
        });
    }

    private void wxPay() {
        AppConst.ORDER_TYPE = 5;
        Map<String, String> map = new HashMap<>();
        map.put("order_id", orderId);
        LogUtil.i(map.toString());
        mProgressDialog.show();
        OkHttpUtil.getInstance().post(this, AppConst.URL_WX_PAY, map, new IResponse() {
            @Override
            public void onSuccess(JsonElement element) {
                mProgressDialog.dismiss();
                LogUtil.i(element.toString());
                Gson gson = new Gson();
                WxPayData wxPayData = gson.fromJson(element, WxPayData.class);
                IWXAPI api = WXAPIFactory.createWXAPI(BuyCoinsActivity.this, AppConst.WEIXIN_APP_ID);
                api.registerApp(AppConst.WEIXIN_APP_ID);
                PayReq payReq = new PayReq();
                payReq.appId = AppConst.WEIXIN_APP_ID;
                payReq.partnerId = wxPayData.getPartnerid();
                payReq.prepayId = wxPayData.getPrepayid();
                payReq.packageValue = "Sign=WXPay";
                payReq.nonceStr = wxPayData.getNoncestr();
                payReq.timeStamp = "" + wxPayData.getTimestamp();
                payReq.sign = wxPayData.getSign();
                api.sendReq(payReq);
            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                LogUtil.i(errorMsg + "statusCode" + statusCode);
                showToast(errorMsg);
                mProgressDialog.dismiss();
            }
        });
    }
}
